home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char SccsId[]= "@(#)text_editor.c V1.26 3/13/95";
- #endif
- /*
- | file name - text_editor.c
- |===================================================================
- |
- | This is an example program that demonstrates how to use a
- | textedit input object. It shows how to load a file into the
- | the editor and save the edited text back into a file.
- | You can type text and save it into a file, or you can load
- | text from a file, edit it, and save it back into the same file
- | or into a different one.
- | To save the text editor's current content into a file, type
- | the filename into the single line text entry field and
- | select the "Save" button.
- | To load a file into the editor, type the filename of the file
- | to load and select the "Load" button.
- | After you save or load a file, a "Saved," "Loaded," or
- | "Error" message appears.
- | To exit from the program, select the "Quit" button.
- | To display help information about the editor commands, select
- | the "Help" button.
- | This example can be modified to suit your needs.
- |
- |===================================================================
- */
- #include <windows.h>
- /*
- * DV-Tools header files
- */
- #include "std.h" /* <stdio.h> etc., scalar & macro definitions */
- #include "dvstd.h" /* public types & constants */
- #include "dvtools.h" /* constants used by T routines */
- #include "dvGR.h" /* constants used by window mgt & GR routines */
- #include "VOstd.h" /* constants used by VO & VOob routines */
- #include "Tfundecl.h" /* T routines (screens, drawports & views) */
- #include "VOfundecl.h" /* VO routines (objects) */
- #include "VUerfundecl.h" /* VUer routines (event handling routines) */
- #include "VGfundecl.h" /* VG routines (get info from dgp & vdp) */
- #ifdef VMS
- #include <file.h>
- #else
- #include <fcntl.h>
- #endif
-
- /* Constants */
- #define DVPATH (char *)NULL
- #define DISPFORMS_STB (char *)NULL
- #define DVDEVICE (char *)NULL
- #define DVCOLORTABLE (char *)NULL
- #define VIEW_NAME "editor.v"
- #define SCREEN_VIEWPORT (RECTANGLE *)NULL
- #define DRAWING_VIEWPORT (RECTANGLE *)NULL
-
- /* Define menu constants */
- #define SAVE 1
- #define LOAD 2
- #define QUIT 3
-
- #define TOO_LONG 2
-
- /* Define global variables */
- float menu_value; /* buffer for menu input object */
- int max_length; /* max. length of string */
- char filename[101] =
- {0}; /* filename to load/save */
- char text_var[1001] =
- {0}; /* buffer for text editor input object */
- int Quit = NO; /* flag to quit the program */
-
- /*
- * Information passed to service result routine
- */
- struct Set
- {
- DRAWPORT drawport;
- OBJECT editor_obj, menu_obj, message_obj, filename_obj;
- };
-
- /* Functions defined in text_editor.c */
- LOCAL int HandleSaveLoad V_P_((OBJECT client, EVENT_REQUEST request,
- int label, OBJECT location, ADDRESS argblock));
- LOCAL int SaveFilename V_P_((void));
- LOCAL int LoadFilename V_P_((void));
- /***************** End Function Declarations *************/
-
- /*
- * MAIN PROGRAM
- */
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow )
- {
- INT argc = 0;
- CHAR **argv;
- /*
- * program arguments
- * argv[1] - display device name (default is to use DVDEVICE)
- */
-
- /* Define & initialize device name and view filename */
- char *device_name = DVDEVICE; /* default device name */
- char *view_name = VIEW_NAME; /* default view name */
-
- /* Define display variables */
- OBJECT screen; /* display device, the window */
- VIEW view; /* picture representation of the view file */
-
- /* Control loop variables */
- OBJECT location; /* the event representation */
-
- /* Input object related variables */
- OBJECT drawing; /* graphical representation of screen */
- ADDRESS *vdplist; /* variable descriptor list for menu */
- int numvars, /* number of vdps in list */
- dim; /* dimension of vdp assigned to text editor */
- struct Set set;
-
- /*--------------------
- * Initialization
- *
- * TInit: perform the initialization of DV-Tools
- * TInit reads your configuration file and any
- * environment variables or logical names set.
- */
- make_argv(&argc,&argv,GetCommandLine());
- TInit( DVPATH, DISPFORMS_STB );
-
- /*
- * TscOpenSet: open a device as a screen object using
- * specified attributes
- *
- * Set exposure block to YES to insure the window
- * is ready for drawing when TdpDraw is called.
- */
- if (argc > 1)
- device_name = argv[1];
- screen = TscOpenSet (device_name, DVCOLORTABLE,
- V_WINDOW_NAME, "text_editor",
- V_X_EXPOSURE_BLOCK, YES,
- V_ACTIVE_CURSOR, V_END_OF_LIST);
- if (!screen)
- {
- printf ("Must specify device on command line or");
- printf (" in DataViews configuration file.\n");
- S_EXIT (EXIT_ERR);
- }
-
- /*
- * VOscWinEventMask: sets the screen's window event mask
- */
- VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS |
- V_MOTIONNOTIFY | V_EXPOSE | V_RESIZE,
- (ULONG) 0);
-
- /*
- * TviLoad: Load a view in from a file
- * TdpCreate: Create a DV-Tools window, a drawport.
- * The drawport is attached to the screen object
- * specified while view specifies the view to be
- * displayed on the screen.
- */
- view = TviLoad (view_name);
- if (!view)
- {
- printf ("Could not load view from file ");
- printf ("%s.\n", view_name);
- S_EXIT (EXIT_ERR);
- }
- set.drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
-
- /*
- * TviGetDrawing: Gets a view's drawing object
- * TdrGetNamedObject: Gets a named object from a
- * drawing.
- *
- * Get objects from the view.
- */
- drawing = TviGetDrawing (view);
- set.editor_obj = TdrGetNamedObject (drawing, "editor");
- set.menu_obj = TdrGetNamedObject (drawing, "menu");
- set.filename_obj = TdrGetNamedObject (drawing, "filename");
- set.message_obj = TdrGetNamedObject (drawing, "message");
-
- /*
- * VOinGetVarList: Gets a variable descriptor list of
- * the input object.
- * TvdPutBuffer: Sets a new variable descriptor buffer
- *
- * Get the variables associated with the menu input object
- * used for saving, loading and quiting as well as the variable
- * associated with the text entry input object used to
- * enter the filename for loading and saving.
- */
- VOinGetVarList (set.menu_obj, &vdplist, &numvars);
- TvdPutBuffer (vdplist[0], (ADDRESS) & menu_value);
- VOinGetVarList (set.filename_obj, &vdplist, &numvars);
- TvdPutBuffer (vdplist[0], filename);
-
- /*
- * VGvddim: Gets the dimensions of a variable descriptor
- *
- * Get the vdp from the text editor and set a new buffer for
- * the vdp. Determine the maximum lenght of the character
- * string allowed in the multi line text editor buffer.
- */
- VOinGetVarList (set.editor_obj, &vdplist, &numvars);
- VGvddim (vdplist[0], &dim, &dim, &max_length);
- TvdPutBuffer (vdplist[0], text_var);
-
- /*
- * VUerServiceResultPost: Post a service result request
- * with the event handler.
- *
- * Post a service result request which monitors the
- * text menu input object. The request specifies the
- * type of the service result flag to be generated as
- * an INPUT_DONE. INPUT_DONE indicates an input sequence
- * has been completed (a menu selection was made).
- */
- VUerServiceResultPost ((OBJECT)1, (VUERFCNFUNPTR)HandleSaveLoad, (ADDRESS) & set,
- (int)sizeof (set), set.menu_obj, (int)INPUT_DONE, (int)0);
-
- /*
- * TdpDraw: Draw the contents of the drawport
- */
- TdpDraw (set.drawport);
-
- /*--------------------
- * Control loop
- *
- * Poll the event queue for locator events. If the key
- * represents the character 'q' or 'Q' or the right
- * mouse button then quit the program. Meanwhile,
- * continue to update the data buffers and draw the
- * next iteration of the drawport.
- */
- FOREVER
- {
- /*
- * VOscWinEventPoll: Poll for the next window event.
- * The polling mode used is V_WAIT.
- * Therefore, VOscWinEventPoll does not
- * return until a masked event is
- * generated. V_WAIT always produces
- * a valid location object.
- */
- location = VOscWinEventPoll (V_WAIT);
-
- /*
- * VOloType: returns the type of event. These types
- * match event types specified in VOscWinEventMask.
- */
- switch (VOloType (location))
- {
-
- case V_RESIZE:
- /*
- * The window size has been changed.
- * TscReset: Resets all screen drawports after
- * window resizing
- */
- TscReset (screen);
- break;
-
- case V_EXPOSE:
- /*
- * VOloRegion: Returns a rectangle representing the
- * exposed region on the screen.
- * TscRedraw: After erasing, redraws all the drawports
- * in the screen.
- * A portion of the window has been exposed and needs
- * to be redrawn.
- */
- TscRedraw (screen, VOloRegion (location));
- break;
-
- case V_KEYPRESS:
- case V_BUTTONPRESS:
- case V_MOTIONNOTIFY:
- VUerHandleLocEvent (location);
- /* Updates editor object only if it was changed after loading
- the new text. */
- TdpDrawNext (set.drawport);
- break;
-
- default:
- break;
- }
- if (Quit == YES)
- break;
- }
-
- /*--------------------
- * Termination
- *
- * TscErase: Erase the entire screen in the default
- * background color
- * TdpDestroy: Destroy the drawport,
- * TviDestroy: Destroy the view, freeing the allocated memory
- * TscCloseCurrentScreen: Close the current display screen
- * TTerminate: Perform the clean-up for DV-Tools
- */
- TscErase (screen);
- TdpDestroy (set.drawport);
- TviDestroy (view);
- TscCloseCurrentScreen ();
- TTerminate ();
- return EXIT_OK;
- }
-
- /*-----------------
- * HandleSaveLoad -- Check users selection for loading or saving
- * multiple lines of text entered by the user.
- */
- /*ARGSUSED */
- LOCAL int
- HandleSaveLoad (client, request, label, location, argblock)
- OBJECT client;
- EVENT_REQUEST request;
- int label;
- OBJECT location;
-
- ADDRESS argblock;
- {
- int saveload;
- char *message = NULL;
- struct Set *set = (struct Set *)argblock;;
-
- /*
- * Check selection made by user to either save or load.
- */
- switch ((int) menu_value)
- {
- case SAVE:
- saveload = SaveFilename ();
- if (saveload)
- message = "File was saved successfully";
- else
- message = "Warning: error occurred when saving file";
- break;
-
-
- case LOAD:
- saveload = LoadFilename ();
- if (saveload == DV_SUCCESS)
- message = "File was loaded successfully";
- else if (saveload == TOO_LONG)
- message = "File is too long, only part was loaded. Don't save it!";
- else
- message = "Warning: error occurred when loading file";
- break;
-
- case QUIT:
- Quit = YES;
- return INPUT_DONE;
- }
-
- if (saveload != DV_FAILURE) /* To set an initial content to restore to */
- TdpDrawObject (set->drawport, set->editor_obj);
-
- /* Display message string to user */
- TdpEraseObject (set->drawport, set->message_obj);
- VOvtSetString (set->message_obj, message);
- TdpDrawObject (set->drawport, set->message_obj);
-
- return INPUT_DONE;
- }
-
-
- /*---------------
- * SaveFilename -- Save text entered by user in text edit
- * input object in specified file.
- */
- LOCAL int
- SaveFilename ()
- {
- FILE *fp;
-
- if (strcmp (filename, "") == 0)
- return DV_FAILURE;
- fp = fopen (filename, "w");
- if (fp == (FILE *) 0)
- return DV_FAILURE;
-
- fwrite (text_var, strlen (text_var), 1, fp);
- fclose (fp);
- return DV_SUCCESS;
- }
-
-
- /*---------------
- * LoadFilename -- Load text file specified by user
- * for loading. Read the text information from the file.
- */
- LOCAL int
- LoadFilename ()
- {
- int num_bytes_read, load_result;
- FILE *fp;
-
- /*
- * Check the filename to check that the filename is not null
- * and the attempt to open the file for reading only.
- */
- if (strcmp (filename, "") == 0)
- return DV_FAILURE;
- fp = fopen (filename, "r");
- if (fp == (FILE *) 0)
- return DV_FAILURE;
-
- /*
- * Read the file for the max number of bytes
- * that can be stored in the buffer text_var. Then close
- * the file.
- */
-
- num_bytes_read = fread (text_var, 1, max_length, fp);
-
- if (num_bytes_read == max_length)
- {
- num_bytes_read -= 1;
- load_result = TOO_LONG;
- }
- else
- load_result = DV_SUCCESS;
-
- text_var[num_bytes_read] = '\0';
- fclose (fp);
-
- return load_result;
- }
-